package test.sign;

import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;

/**
 * 
 * @author Jerry
 */
public class EasyMD5 {

	private String digeststring;

	private static int MAX_BUFFER_SIZE = 4096;

	public EasyMD5(File fi) {
		long updated = 0;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			FileInputStream in = new FileInputStream(fi);
			while (updated < fi.length()) {
				byte[] buffer;
				if (fi.length() - updated >= MAX_BUFFER_SIZE) {
					buffer = new byte[MAX_BUFFER_SIZE];
					in.read(buffer);
				} else {
					buffer = new byte[(int) (fi.length() - updated)];
					in.read(buffer);
				}
				md.update(buffer);
				updated += buffer.length;
			}
			in.close();
			byte[] b = md.digest();
			StringBuilder r = new StringBuilder();
			for (int i = 0; i < b.length; i++) {
				String temp = Integer.toHexString(b[i] < 0 ? b[i] + 256 : b[i]);
				r.append(temp.length() == 1 ? "0" + temp : temp);
			}
			digeststring = r.toString();
		} catch (Exception e) {
		}
	}

	@Override
	public String toString() {
		return digeststring;
	}
}
